package com.xuecheng.media.controller;

import com.xuecheng.api.media.model.MediaApi;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {
    @Autowired
    private MediaService  mediaService;
    /*
    todo 存储媒资
    */
    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody  MediaVO vo) {
        //获取机构id
        Long companyId = SecurityUtil.getCompanyId();
        MediaDTO mediaDTO = MediaConvert.INSTANCE.vo2dto(vo);
        MediaDTO media = mediaService.createMedia(mediaDTO);
        media.setCompanyId(companyId);
        return media;
    }

    /*
    todo 媒资分页条件查询
     */
    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {
        Long companyId = SecurityUtil.getCompanyId();
        PageVO pageVO = mediaService.queryMediaList(params, model, companyId);

        return pageVO;
    }
// 媒资预览
    @GetMapping("/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        String vodUrl = mediaService.getVODUrl(mediaId, companyId);
        return vodUrl;
    }
//删除
@DeleteMapping("/media/{mediaId}")
    public void removeMedia(@PathVariable  Long mediaId) {
    Long companyId = SecurityUtil.getCompanyId();
        mediaService.removeMedia(mediaId,companyId);

    }
//为服务远程掉用,课程计划绑定媒资信息
   @GetMapping("l/media/{mediaId}")
    public RestResponse<MediaDTO> getMediaById4s(@PathVariable  Long mediaId) {
       RestResponse<MediaDTO> mediaById4s = mediaService.getMediaById4s(mediaId);
       return mediaById4s ;
    }

}
